package listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

//@WebListener
public class 会话创建销毁监听 implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        Date date = new Date();
        session.setMaxInactiveInterval(1);
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        System.out.println(session.getId()+"在时间："+sdf.format(date)+"被创建啦");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session = httpSessionEvent.getSession();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
        System.out.println(session.getId()+"在时间："+sdf.format(date)+"被销毁啦");
    }
}
